import request from './request';
// import qs from 'qs';
const baseUrl = window.appcfg._servicesUrl;

// import moment from 'moment';

// 设置基本的opthions
function setOptions(method = 'GET', param = {}) {
  let query = '';
  // const token = localStorage.getItem('ys7')
  // const token1 = 'eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJsZW9wYXJkIiwianRpIjoiMzAiLCJzdWIiOiJzdXBlckFkbWluIiwiaWF0IjoxNTk2NTA0Nzk1LCJleHAiOjE1OTY1NDc5OTV9.vNqHMREAtt9xLHYNM3HozMWvvSf0L4VtEbPvhV6IrnMFenjeaXu-IOHLojGNtNHfxFcSzINzQfnzbWqpMpRaOg'
  const options = {
    method: method.toUpperCase(),
    headers: {
      'Content-Type': 'application/json',
    },
  };
  if (method.toLowerCase() !== 'get') {
    options.body = JSON.stringify(param);
  }
  // else if (method.toLowerCase() === 'get') {
  //   query = qs.stringify(param);
  // }
  return options;
}

/**
 * 项目相关
 */

// 获取项目列表
export function getProjList() {
  let options = setOptions();
  return request(`${baseUrl}/api/projects/top-level`, options);
}
// 增加项目
export function addProj({ name, code }) {
  let options = setOptions('post', {
    code,
    name,
    pid: 0,
  });
  return request(`${baseUrl}/api/projects`, options);
}

// 删除项目
export function deleteProj(objectId) {
  let options = setOptions('delete');
  return request(`${baseUrl}/api/projects${objectId}`, options);
}

// 更新项目
export function updateProj({ name = '', code = '', objectId }) {
  let options = setOptions('put', {
    code,
    name,
    objectId,
  });
  return request(`${baseUrl}/api/projects`, options);
}

// 根据code获取bim模型内容
export function getModelByCode(code) {
  let options = setOptions();
  return request(`${baseUrl}/api/projects/code/${code}`, options).then(data => {
    // TODO 服务迁移端口
    const _data = { data: { ...data.data } };
    _data.data.modelList = _data.data.modelList.map(e => {
      return {
        ...e,
        url: e.url.replace('https://portal.wccg.tech/', 'https://portal.wccg.tech:9002/to443/'),
      };
    });
    console.log(_data, '+++---------------------');
    return _data;
  });
}
// 根据code获取影像服务内容
export function getImgServiceByCode(code) {
  let options = setOptions();
  return request(`${baseUrl}/api/images/code/${code}`, options).then(data => {
    // TODO 服务迁移端口
    const _data = { data: { ...data.data } };
    _data.data.modelList = _data.data.modelList.map(e => {
      return {
        ...e,
        url: e.url.replace('https://portal.wccg.tech/', 'https://portal.wccg.tech:9002/to443/'),
      };
    });
    console.log(_data, '+++---------------------');
    return _data;
  });
}

/**
 * 模型相关
 */

// 获取项目下的模型列表
export function getModelListByProjId(id) {
  let options = setOptions();
  return request(`${baseUrl}/api/projects/${id}`, options);
}

// 添加模型
export function addModelToProjId({ label = '', menuId, name, url = '' }) {
  let options = setOptions('post', { label, menuId, name, url });
  return request(`${baseUrl}/api/models`, options);
}
// 更新模型
export function updateModel({ label = '', menuId, name, url = '', objectId, visible }) {
  let options = setOptions('put', { label, menuId, name, url, objectId, visible });
  return request(`${baseUrl}/api/models`, options);
}
// 删除模型
export function deleteModels(ids = []) {
  let options = setOptions('delete', ids);
  return request(`${baseUrl}/api/models`, options);
}
/**
 * 影像服务相关
 */

// 获取项目下的影像服务列表
export function getServicesListByProjId(id) {
  let options = setOptions();
  return request(`${baseUrl}/api/images/menu-id/${id}`, options);
}

// 添加影像服务
export function addServiceToProjId({ menuId, name, url = '' }) {
  let options = setOptions('post', { visible: true, menuId, name, url });
  return request(`${baseUrl}/api/images`, options);
}
// 更新影像服务
export function updateService({ label = '', menuId, name, url = '', objectId, visible }) {
  let options = setOptions('put', { label, menuId, name, url, objectId, visible });
  return request(`${baseUrl}/api/images`, options);
}
// 删除影像服务
export function deleteServices(ids = []) {
  let options = setOptions('delete', ids);
  return request(`${baseUrl}/api/images`, options);
}

/**
 * 工区管理
 */
// 获取当前项目下工区
export function getEquipmentsByProjId(code) {
  let options = setOptions();
  return request(
    `http://wccg.tech:88/api/wuxi/api/project/project/equipments?projectId=${code}`,
    options,
  );
}

// 添加设备
export function addEquipmentToProjId({ code, name, online = 0, offline = 0 }) {
  let options = setOptions('post', { offline, code, name, online });
  return request(`${baseUrl}/api/equipments`, options);
}
// 添加设备
export function updateEquipment({ code, name, online = 0, offline = 0, objectId }) {
  let options = setOptions('put', { offline, code, name, online, objectId });
  return request(`${baseUrl}/api/equipments`, options);
}

// 删除设备
export function deleteEquipment(objectId) {
  let options = setOptions('delete');
  return request(`${baseUrl}/api/equipments/${objectId}`, options);
}
